# encoding=utf8

import urllib.request, urllib.parse, urllib.error
import socket

socket.setdefaulttimeout(3)
f = open("./proxy")
lines = f.readlines()
proxys = []
for i in range(0, len(lines)):
    ip = lines[i].strip("\n").split("\t")
    proxy_host = "http://" + ip[0] + ":" + ip[1]
    proxy_temp = {"http": proxy_host}
    proxys.append(proxy_temp)
url = "http://ip.chinaz.com/getip.aspx"
for proxy in proxys:
    proxy_handler = urllib.request.ProxyHandler(proxy)
    # proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
    # proxy_auth_handler.add_password('realm', '123.123.2123.123', 'user', 'password')
    opener = urllib.request.build_opener(urllib.request.HTTPHandler, proxy_handler)

    try:
        f = opener.open('http://www.baidu.com')
        res = f.read()
        print(res)
    except Exception as e:
        print(proxy)
        print(e)
        continue
